<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>登入</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="keywords" content="fly,layui,前端社区">
  <meta name="description" content="Fly社区是模块化前端UI框架Layui的官网社区，致力于为web开发提供强劲动力">
  <link rel="stylesheet" th:href="@{/res/layui/css/layui.css}">
  <link rel="stylesheet" th:href="@{/res/css/global.css}">
</head>
<body>

<div th:replace="/common/header::header-div"></div>

<div class="layui-container fly-marginTop">
  <div class="fly-panel fly-panel-user" pad20>
    <div class="layui-tab layui-tab-brief" lay-filter="user">
      <ul class="layui-tab-title">
        <li class="layui-this">登入</li>
        <li><a href="/user/reg">注册</a></li>
      </ul>
      <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
        <div class="layui-tab-item layui-show">
          <div class="layui-form layui-form-pane">
            <form method="post">
              <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                  <input type="text" id="L_email" name="email" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                  <i class="layui-icon" id="email_r" style="color: green;font-weight: bolder;" hidden></i>
                </div>
                <div class="layui-form-mid layui-word-aux">
                  <i class="layui-icon" id="email_l" style="color: red; font-weight: bolder;" hidden>ဆ</i>
                </div>
              </div>
              <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                  <input type="password" id="L_pass" name="pass" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                  <i class="layui-icon" id="pass_r" style="color: green;font-weight: bolder;" hidden></i>
                </div>
                <div class="layui-form-mid layui-word-aux">
                  <i class="layui-icon" id="pass_l" style="color: red; font-weight: bolder;" hidden>ဆ</i>
                </div>
              </div>
              <div class="layui-form-item">
                <button class="layui-btn" lay-filter="sub" lay-submit>立即登录</button>
                <span style="padding-left:20px;">
                  <a href="forget.html">忘记密码？</a>
                </span>
              </div>
              <div class="layui-form-item fly-form-app">
                <span>或者使用社交账号登入</span>
                <a href="" onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})" class="iconfont icon-qq" title="QQ登入"></a>
                <a href="" onclick="layer.msg('正在通过微博登入', {icon:16, shade: 0.1, time:0})" class="iconfont icon-weibo" title="微博登入"></a>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="fly-footer">
  <p><a href="http://fly.layui.com/" target="_blank">Fly社区</a> 2017 &copy; <a href="http://www.layui.com/" target="_blank">layui.com 出品</a></p>
  <p>
    <a href="http://fly.layui.com/jie/3147/" target="_blank">付费计划</a>
    <a href="http://www.layui.com/template/fly/" target="_blank">获取Fly社区模版</a>
    <a href="http://fly.layui.com/jie/2461/" target="_blank">微信公众号</a>
  </p>
</div>

<div th:replace="/common/translate::translate-div"></div>

<script th:src="@{/res/layui/layui.js}"></script>
<script>
  layui.use(['form','jquery','layer'], function () {
    const form = layui.form;
    const $ = layui.jquery;
    const layer = layui.layer;

    $("#L_email").blur(function () {
      if($('#L_email').val() == "") return;
      let judge = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if(!($('#L_email').val().match(judge))){
        $('#email_l').removeAttr('hidden');
        $('#email_r').attr('hidden','hidden');
        layer.msg('请输入合法邮箱');
      }else{
        $('#email_r').removeAttr('hidden');
        $('#email_l').attr('hidden','hidden');
      }
    });

    $("#L_pass").blur(function () {
      if($('#L_pass').val() == "") return;
      let judge = /^[0-9a-zA-Z]{6,16}$/;
      if(!($('#L_pass').val().match(judge))){
        $('#pass_l').removeAttr('hidden');
        $('#pass_r').attr('hidden','hidden');
        layer.msg('请输入规定密码');
      }else{
        $('#pass_r').removeAttr('hidden');
        $('#pass_l').attr('hidden','hidden');
      }
    });

    form.on('submit(sub)', function(data) {
      if(!new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$").test(data.field.email)){
        layer.msg("请输入有效邮箱");
        return false;
      }
      if(!new RegExp("^[0-9a-zA-Z]{6,16}$").test(data.field.password)){
        layer.msg("请输入有效密码");
        return false;
      }
      $.ajax({
        url:'/user/login',
        data:JSON.stringify(data.field),
        contentType: "application/json",
        dataType:'json',
        type:'post',
        success:function(data){
          if (data.success) {
            layer.msg("登录成功");
            location.href = "/index";
          }else {
            layer.msg("登录失败");
          }
        }
      });
      //防止页面跳转
      return false;
    });

    window.translation = function (that) {
      let word = $(that).parent().prev().children().val() ;
      if(word == "" ) return;
      $.ajax({
        url:'/learn/word/one/' + word,
        dataType:"json",
        type:'get',
        success:function(data){
          if(data.success){
            let english = data.data.english;
            let chinese = data.data.chinese;
            $("#english").html(english);
            $("#chinese").html(chinese);
            layer.open({
              type: 1,
              title: '单词信息',
              content: $('#tr')//这里content是一个普通的String
            });
          }else{
            layer.msg("单词不存在")
            return;
          }
        }
      });
    };
  })
</script>

</body>
</html>